//
//  LSOpinionView.m
//  ZLFastDoctor
//
//  Created by 李宗帅 on 16/5/22.
//  Copyright © 2016年 LS. All rights reserved.
//

#import "LSOpinionView.h"
#import <Masonry.h>
#import <SVProgressHUD.h>
#define LSOpinionMargin 10
@interface LSOpinionView ()<UITextViewDelegate>

//建议
@property (nonatomic,strong) UITextView *label;

//剩余字数提示
@property (nonatomic,strong) UILabel *textTitle;
//发送按钮
@property (nonatomic,strong) UIButton *sendBtn;

@end

@implementation LSOpinionView

+ (instancetype)toHeardView {
    LSOpinionView *view = [[LSOpinionView alloc]init];
    
    return view;
}


//加载控件
- (instancetype)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {
        
        [self setupUI];
    }
    
    return self;
}

//加载控件
- (void)setupUI {
    
    

    self.label.delegate = self;
    
    self.label.text = @"";
    
    self.label.textColor = [UIColor blackColor];
        
    self.label.textAlignment = NSTextAlignmentLeft;
    
    self.label.font = [UIFont systemFontOfSize:13];
    
    // 当textView拖拽的时候 让键盘消失，只设置 onDarg是不行,还有下面一句话
    self.label.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    //设置一下,textView的滚动方向
    self.label.alwaysBounceVertical = YES;
    
    [self addSubview:self.label];
    
    
    self.textTitle.text = @"您还可以输入200个字";
    
    self.textTitle.textColor = [UIColor blackColor];
    
    self.textTitle.textAlignment = NSTextAlignmentCenter;
    
    self.textTitle.font = [UIFont boldSystemFontOfSize:15];
    
    [self addSubview:self.textTitle];
    
    [self.sendBtn setBackgroundImage:[UIImage imageNamed:@"nav"] forState:UIControlStateNormal];
    
    [self.sendBtn setTitle:@"递交反馈" forState:UIControlStateNormal];
    
    self.sendBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    
    [self.sendBtn addTarget:self action:@selector(clickSentBtn) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:self.sendBtn];

}

- (void)layoutSubviews {
    
    [self.sendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.right.equalTo(self).offset(-LSOpinionMargin);
        
        make.height.equalTo(@30);
        
        make.width.equalTo(@60);
    }];
    
    [self.textTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.sendBtn);
        
        make.centerX.equalTo(self).offset(-50);
    }];
    
    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self);
        
        make.right.bottom.equalTo(self);
    }];
    
}


//监听textView的改变
- (void)textViewDidChange:(UITextView *)textView {
    
    NSInteger num = textView.text.length;
    
    num = 200 - num;
    
    self.textTitle.text = [NSString stringWithFormat:@"您还可以输入%ld个字",(long)num];
    
    self.textTitle.textColor = [UIColor redColor];
    
    [self layoutIfNeeded];
}

//递交按钮的点击方法实现
- (void)clickSentBtn {
    
    NSString *title = self.label.text;
    
        if ([self.delegate respondsToSelector:@selector(clickSentBtnWithContentText:)]) {
            [self.delegate clickSentBtnWithContentText:title];
        }
    self.label.text = @"";
}

//懒加载控件
- (UITextView *)label {
    if (_label == nil) {
        _label = [[UITextView alloc]init];
    }
    
    return _label;
}

- (UILabel *)textTitle {
    if (_textTitle == nil) {
        _textTitle = [[UILabel alloc]init];
    }
    
    return _textTitle;
}

- (UIButton *)sendBtn {
    if (_sendBtn == nil) {
        _sendBtn = [[UIButton alloc]init];
    }
    
    return _sendBtn;
}
@end
